﻿using System;
using System.Windows;
using System.Windows.Threading;
using mal.WP7.Framework.Controls;
using mal.WP7.GoogleAnalytics.Business.Repository;
using mal.WP7.GoogleAnalytics.Business.Resources;
using mal.WP7.GoogleAnalytics.ViewModels;
using Microsoft.Phone.Shell;
//using mal.WP7.GoogleAnalytics.Business.Resources;

namespace mal.WP7.GoogleAnalytics
{
    public partial class ConfigurationPage : NavigatePhoneApplicationPage
    {
        #region Fields
        private const int AddButtonIndex = 0;
        private const int HomePageButtonIndex = 1;
        ////private const int CloseButtonIndex = 1;
 
        private ConfigurationViewModel viewModel;
        private Account account;
        private DispatcherTimer timer;
        #endregion

        #region Construction / Destruction
        public ConfigurationPage()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(this.ConfigurationPage_Loaded);
            this.viewModel = new ConfigurationViewModel();
            this.DataContext = this.viewModel;

            this.timer = new DispatcherTimer();
            this.timer.Interval = TimeSpan.FromSeconds(1);
            this.timer.Tick += new EventHandler(this.OntimerTick);
        }             
        #endregion

        #region enum, struct, class

        #endregion

        #region Properties

        #endregion

        #region Methods

        #endregion

        #region Events
        private void ConfigurationPage_Loaded(object sender, RoutedEventArgs e)
        {
            ((ApplicationBarIconButton)this.ApplicationBar.Buttons[HomePageButtonIndex]).Text = ResourceText.HomePageButton;
            ((ApplicationBarIconButton)this.ApplicationBar.Buttons[AddButtonIndex]).Text = ResourceText.ConfigurationPageAddButton;

            this.viewModel.LoadAccounts();
        } 

        private void OnAddAccountClick(object sender, EventArgs e)
        {
            this.viewModel.AddAccount();
        }

        ////private void OnCloseClick(object sender, EventArgs e)
        ////{
        ////    this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        ////}

        private void TextBlock_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.account = ((FrameworkElement)sender).DataContext as Account;

            if (this.timer.IsEnabled)
            {
                this.timer.Stop();
            }

            if (this.account != null)
            {
                this.timer.Start();
            }
        }

        private void TextBlock_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (this.timer.IsEnabled)
            {
                this.timer.Stop();
            }
        }

        private void OntimerTick(object sender, EventArgs e)
        {
            this.timer.Stop();
            if (this.account != null)
            {
                MessageBoxResult result = MessageBox.Show(ResourceText.RemoveAccountMessage
                    , ResourceText.RemoveAccountCaption
                    , MessageBoxButton.OKCancel);

                if (result == MessageBoxResult.OK)
                {
                    WebsiteRepository.Instance.RemoveAccount(this.account);
                    this.viewModel.LoadAccounts();
                }
            }
        }

        private void HomeButton_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
        }
        #endregion
    }
}